﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_ToDo_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            txtStartdate.Text = Now.ToString("dd/MM/yyyy")
            LoadListHour(ddlbeHour)
            ddlbeHour.SelectedIndex = Now.Hour
            LoadListHour(ddlDueDateHour)
            LoadListHour(ddlRemindDateHour)

            '  ddleHour.SelectedIndex = 23
            LoadListMinutes(ddlbeMinutes)
            LoadListMinutes(ddlDueDateMinute)
            LoadListMinutes(ddlRemindDateMinute)

            ddlbeMinutes.SelectedIndex = Now.Minute
            '   LoadListMinutes(ddleMinutes)
            '   ddleMinutes.SelectedIndex = 59

        End If
    End Sub
    Sub LoadListHour(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 23
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Sub LoadListMinutes(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 59
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Protected Sub ResetControl()
        txtSubject.Text = ""
        txtDescription.Text = ""
        txtStartdate.Text = ""
        txtDuedate.Text = ""
        txtReminddate.Text = ""

    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As New VSModel.ToDoList
            Try

                p.Subject = txtSubject.Text.Trim()
                p.Description = txtDescription.Text.Trim()
                p.StartDate = StringToDateHour(txtStartdate.Text, ddlbeHour.SelectedValue, ddlbeMinutes.SelectedValue)
                p.DueDate = StringToDateHour(txtDuedate.Text.Trim(), ddlDueDateHour.SelectedValue, ddlDueDateMinute.SelectedValue)
                p.RemindDate = StringToDateHour(txtReminddate.Text.Trim(), ddlRemindDateHour.SelectedValue, ddlRemindDateMinute.SelectedValue)
                p.NumberRemind = 1
                p.Status = 1
                p.IsActive = chkIsActive.Checked
                p.UserId = Session("UserId")

                data.ToDoLists.AddObject(p)
                data.SaveChanges()
                hidID.Value = p.Id
                'Insert_App_Log("Insert  Todolist:" & txtTitle.Text.Trim & "", Function_Name.Todolist, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                Excute_Javascript("Alertbox('Nhập dữ liệu thành công.'); window.location.href='" & "Detail.aspx?Id=" & hidID.Value & "'", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using

    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub



#End Region
End Class
